const { getHttpCart } = require("../../models/shop");

// pages/vant/vant.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isChecked:true,
    carts:[],
    sum:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
/*   onLoad: function (options) {

  }, */
  onLoad() {
    getHttpCart({
      success: res => {
        this.setData({
          carts: res.data
        })
      }
    })
  },
  onCount(event) {
    /* 1.监听商品数量的改变,得到小计的价格 productCount */
    var carts = this.data.carts;
    var count = event.detail;
    var id = event.currentTarget.dataset.id;
    var item = carts.find(item => item.id == id);
    item.productCount = count;
    // console.log(carts);
    this.setData({
      carts
    })
  },
  onItemChange(event){
    /* 2.改变对应item的checkbox的状态 */
    /* var carts = this.data.carts;
    var state = event.detail;
    var id = event.currentTarget.dataset.id;
    var item = carts.find(item=> item.id ==id);
    item.isSelected = state;
    this.setData({
      carts
    }) */
    // console.log(event.detail);
    this.handleItem("isSelected",event)
  },
  handleItem(attr,event){
    var carts = this.data.carts;
    var value = event.detail;
    var id = event.currentTarget.dataset.id;
    var item = carts.find(item=> item.id ==id);
    item[attr] = value;
    this.setData({
      carts
    })
  },
  onSum(event){
    var carts = this.data.carts;
    var value = event.detail;
    console.log(value);
    carts.forEach(item=>{
      item.isSelected = value;
    })
    this.setData({
      carts
    })
  },
  onChange(event){
    this.setData({
      isChecked:event.detail
    })
    // console.log(event.detail);
  },
  handleChange(event){
    /* 原生的组件是没有bind:Change这个事件 */
    // console.log(event.detail);
    this.setData({
      isChecked:!this.data.isChecked
    })
  },
  onSearch(event){
    // console.log(event.detail);
  }
  
})